package tool;

import java.awt.Color;

import com.base.service.AddressService;
import com.wechat.service.WechatService;

public class UploadImg {

	/** 通过serverId从微信服务器下载图片到本地
	 *  @param args
	 */
	
//	private static String imgLocalFolder="/usr/local/tomcat7/webapps/microhifo/upload/picture";	//阿里云上
//	private static String tmpFolder="/usr/local/tomcat7/webapps/microhifo/upload/tmp";
	
	private static String imgLocalFolder="D:\\java\\tomcat7056\\webapps\\microhifo\\upload\\picture";	//上传图片下载到本地的文件夹
	private static String tmpFolder="D:\\java\\tomcat7056\\webapps\\microhifo\\upload\\tmp";		//临时文件夹
	
	private static String _url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
	
	
	
	
	/**
	 * 使用同步锁
	 * 
	 * 生成文件的命名规则为：
	 * 
	 *[creatorid+currentTime+.jpg]
	 * waterMark:
	 * currentTime+gpsName
	 * 
	 * @return fail:-1  success:1
	 */
	public static synchronized int upload(Integer creator,String accessToken,String mediaId,String latitude,String longitude) throws Exception{
	try{	
		String AddrName=AddressService.getInstance().getNameByGps(latitude,longitude);
		String url;
		
		String fileName=creator.toString()+"_"+new Long(System.currentTimeMillis()).toString()+".jpg";
		
		String timeStr=TimeTool.getNowTime()+"  "+TimeTool.getWeekDay();;
		url=_url.replace("ACCESS_TOKEN", accessToken);
		url=url.replace("MEDIA_ID", mediaId);
		
		DownImg.download(url, fileName, tmpFolder);
		WaterMarkUtils.getInstance().mark(tmpFolder+"\\"+fileName, imgLocalFolder+"\\"+fileName, Color.red, AddrName,timeStr);
		
		return 1;
		
	}catch(Exception e){
		e.printStackTrace();
		return -1;
	}
	
	}
	
	
	//测试方法
	public static int upload() throws Exception{
		String url="";
		String accessToken=WechatService.getInstance().find().getAccessToken();
		String serverId="TDD5K4NTMCJHmEOKtK_Aq2XkNuWziKT5deZJJ3kcF2xVWNcdGCR3QuI_MYQXJbS_";
		String waterMark="四川省成都市成华区龙潭街道成都农商银行(同乐分理处)";
		String timeStr=TimeTool.getNowTime()+"  "+TimeTool.getWeekDay();;
		
		url=_url.replace("ACCESS_TOKEN", accessToken);
		url=url.replace("MEDIA_ID", serverId);
//		System.out.println(url);
		
		String fileName=new Long(System.currentTimeMillis()).toString()+".jpg";
//		download("http://ui.51bi.com/opt/siteimg/images/fanbei0923/Mid_07.jpg", "51bi.gif","/Users/chz/Desktop/tmp");
		DownImg.download(url, fileName, tmpFolder);
		System.out.println(tmpFolder+"\\"+fileName);
		WaterMarkUtils.getInstance().mark(tmpFolder+"\\"+fileName, imgLocalFolder+"\\"+fileName, Color.red, waterMark,timeStr);
		
		//打水印
		
		
		return 1;
	}
	
	
	public static int upload(String imgServerId){
		
		
		return 1;
	}
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		UploadImg.upload();

	}

}
